//
//  AppDelegate.swift
//  PGWeibo
//
//  Created by Apple on 15/11/3.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.rootViewController = defaultRootViewController
        
        window?.makeKeyAndVisible()
        
        //跳到主界面的通知
        NSNotificationCenter.defaultCenter().addObserverForName(PGSwitchRootControllerNotification, object: nil, queue: nil, usingBlock: { (notification) -> Void in
            let vc = notification.object != nil ? PGWelcomeViewController() : PGTabBarViewController()
            // 切换控制器
            self.window?.rootViewController = vc
        })

        //设置全局外观
        setupApppearance()
        return true
    }
    //设置全局渲染
    private func setupApppearance() {
        UINavigationBar.appearance().tintColor = apppearanceTintColor
        UITabBar.appearance().tintColor = apppearanceTintColor
    }	
}

///gen// MARK: - 根控制器的跳转
extension AppDelegate {
    //设置根控制器
    private var defaultRootViewController: UIViewController {
        
//        if PGUserAccountViewModel.sharedUerAccountViewModel.userLogon {
//            return  isNewVersion ? PGNewFeatureViewController() : PGWelcomeViewController()
//        }
//        return PGTabBarViewController()
        if isNewVersion {
            return PGNewFeatureViewController()
        }
        
        return PGWelcomeViewController()
    }
    
    private var isNewVersion: Bool {
        //当前版本
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let sandBoxVersionKey = "CFBundleShortVersionString"
        //从沙盒获取版本
        let sandBoxVersion = NSUserDefaults.standardUserDefaults().objectForKey(sandBoxVersionKey) as? String
        //写入版本到沙盒
        NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: sandBoxVersionKey)
        
        return currentVersion.compare(sandBoxVersion ?? "") == .OrderedDescending
    }
}